using System.Reflection;
using System.Text.Json;
using Hjson;
using RSCore;
using RSCore.Files;
using Terraria.Localization;
using Terraria.ModLoader;

namespace _ReplaceString_;

public class ReplaceString : ModBase
{
	public static ReplaceString Instance { get; private set; } = default!;

	public event Action? OnUnload;

	public ReplaceString()
	{
		Instance = this;
	}

	public override void Load()
	{
		ModHelper.Init(Logger);
	}

	public override void Unload()
	{
		OnUnload?.Invoke();
	}

	public override void PostSetupContent()
	{
		var dir = Directory.CreateDirectory(nameof(ReplaceString));
		var file = new FileInfo(Path.Combine(dir.FullName, "Terraria.hjson"));
		if (!file.Exists)
		{
			var json = TextIO.ToJson(LanguageManager.Instance._localizedTexts
				.Where(p => !p.Key.StartsWith("Mods", StringComparison.Ordinal))
				.Select(p => new KeyValuePair<string, string>(p.Key, p.Value.Value)));
			using var stream = file.CreateText();
			HjsonValue.Save(json, stream, new HjsonOptions()
			{
				EmitRootBraces = false,
				KeepWsc = true,
			});
		}

		// ModInfoCatcher.Load();
	}

	protected override List<string> LoadPath()
	{
		return new List<string>();
	}
}